package utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.io.Serializable;
import java.util.Hashtable;
import java.util.List;

import models.Book;
import models.ClassifiedInfo;
import models.FlowStatistic;
import models.Information;
import models.MemberInfo;
import models.Record;
import models.StatisticInfo;
import models.UserVO;
import others.ServerResponse;

import static com.google.gson.internal.UnsafeAllocator.create;

public class GsonUtil {

    public static String class2Json(Object param){
        //Gson gson = new Gson();
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();
        return gson.toJson(param);
    }

    public static ServerResponse Json2Class(String responseData){
        Gson gson = new Gson();
        ServerResponse serverResponse = gson.fromJson(responseData,ServerResponse.class);
        return serverResponse;
    }

    public static ServerResponse<UserVO> String2UserVoServerResponse(String responseData){
        Gson gson = new Gson();
        ServerResponse<UserVO>  serverResponse = gson.fromJson(responseData,
                new TypeToken<ServerResponse<UserVO>>(){}.getType());
        return serverResponse;
    }

    public static ServerResponse<List<Book>> String2ListBookServerResponse(String responseData){
        Gson gson = new Gson();
        ServerResponse<List<Book>> serverResponse = gson.fromJson(responseData,
                new TypeToken<ServerResponse<List<Book>>>(){}.getType());
        return serverResponse;
    }

    public static ServerResponse<List<StatisticInfo>> String2ListStatisticServerResponse(String responseData){
        Gson gson = new Gson();
        ServerResponse<List<StatisticInfo>> serverResponse = gson.fromJson(responseData,
                new TypeToken<ServerResponse<List<StatisticInfo>>>(){}.getType());
        return serverResponse;
    }

    public static ServerResponse<List<FlowStatistic>> String2ListFlowsServerResponse(String responseData){
        Gson gson = new Gson();
        ServerResponse<List<FlowStatistic>> serverResponse = gson.fromJson(responseData,
                new TypeToken<ServerResponse<List<FlowStatistic>>>(){}.getType());
        return serverResponse;
    }

    public static ServerResponse<ClassifiedInfo> String2ListClassifiedServerResponse(String responseData){
        Gson gson = new Gson();
        ServerResponse<ClassifiedInfo> serverResponse = gson.fromJson(responseData,
                new TypeToken<ServerResponse<ClassifiedInfo>>(){}.getType());
        return serverResponse;
    }

    public static ServerResponse<Hashtable<String,String>> String2HashtableServerResponse(String responseData){
        Gson gson = new Gson();
        ServerResponse<Hashtable<String,String>> serverResponse = gson.fromJson(responseData,
                new TypeToken<ServerResponse<Hashtable<String,String>>>(){}.getType());
        return serverResponse;
    }

    public static ServerResponse<List<Record>> String2ListRecordResponse(String responseData){
        Gson gson = new Gson();
        ServerResponse<List<Record>> serverResponse = gson.fromJson(responseData,
                new TypeToken<ServerResponse<List<Record>>>(){}.getType());
        return serverResponse;
    }

    public static ServerResponse<List<Information>> String2ListInfoServerResponse(String responseData){
        Gson gson = new Gson();
        ServerResponse<List<Information>> serverResponse = gson.fromJson(responseData,
                new TypeToken<ServerResponse<List<Information>>>(){}.getType());
        return serverResponse;
    }

    public static ServerResponse<List<MemberInfo>> String2ListMemberInfo(String responseData){
        Gson gson = new Gson();
        ServerResponse<List<MemberInfo>> serverResponse = gson.fromJson(responseData,
                new TypeToken<ServerResponse<List<MemberInfo>>>(){}.getType());
        return serverResponse;
    }
}
